package com.zkrt.upms.common.util;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.zkrt.upms.enums.IotHJMessageEnum;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class HJ212Util {

    /**
     * ##0101
     * QN=20160801085857223;
     * ST=32;
     * CN=1062;
     * PW=100000;
     * MN=010000A8900016F000169DC0;
     * Flag=5;
     * CP=&&RtdInterval=30&&1C80
     *
     * @param json
     * @return
     * @throws Exception
     */
    public static String getHJString(JSONObject json) throws Exception {
        String cn = json.getString(IotHJMessageEnum.CN.getKey());
        String flag = json.getString(IotHJMessageEnum.FLAG.getKey());
        String mn = json.getString(IotHJMessageEnum.MN.getKey());
        String pw = json.getString(IotHJMessageEnum.PW.getKey());
        String qn = json.getString(IotHJMessageEnum.QN.getKey());
        String st = json.getString(IotHJMessageEnum.ST.getKey());
        JSONObject cp = json.getJSONObject(IotHJMessageEnum.CP.getKey());
        String dataTime = cp.getString(IotHJMessageEnum.CP_DATATIME.getKey());
        String w01001_flag = cp.getString(IotHJMessageEnum.CP_W01001_FLAG.getKey());
        String w01001_rtd = cp.getString(IotHJMessageEnum.CP_W01001_RTD.getKey());
        String w01009_flag = cp.getString(IotHJMessageEnum.CP_W01009_FLAG.getKey());
        String w01009_rtd = cp.getString(IotHJMessageEnum.CP_W01009_RTD.getKey());
        String w01010_flag = cp.getString(IotHJMessageEnum.CP_W01010_FLAG.getKey());
        String w01010_rtd = cp.getString(IotHJMessageEnum.CP_W01010_RTD.getKey());

        StringBuffer message = new StringBuffer();
        if ("0".equals(w01001_rtd)) {
            message.append("\n").append("[w01001阈值异常] -> 0");
        }
        if ("0".equals(w01009_rtd)) {
            message.append("\n").append("[w01009阈值异常] -> 0");
        }
        if ("0".equals(w01010_rtd)) {
            message.append("\n").append("[w01010阈值异常] -> 0");
        }
        if (ObjectUtil.isNotEmpty(message)) { // 阈值异常打印vim
            log.error(message.append("\n").toString());
        }

        StringBuffer sb = new StringBuffer();
        String str = sb.append("QN=").append(qn).append(";")
                .append("ST=").append(st).append(";")
                .append("CN=").append(cn).append(";")
                .append("PW=").append(pw).append(";")
                .append("MN=").append(mn).append(";")
                .append("Flag=").append(flag).append(";")
                .append("CP=").append("&&")
                .append("DataTime=").append(dataTime).append(";")
                .append("w01009-Rtd=").append(w01009_rtd).append(",")
                .append("w01009-Flag=").append(w01009_flag).append(";")
                .append("w01010-Rtd=").append(w01010_rtd).append(",")
                .append("w01010-Flag=").append(w01010_flag).append(";")
                .append("w01001-Rtd=").append(w01001_rtd).append(",")
                .append("w01001-Flag=").append(w01001_flag)
                .append("&&").toString();
        String crc = getCRC16(str);
        int crcLength = crc.length();
        if (crcLength < 4) {
            for (int i = crcLength; i < 4; i++) {
                crc = new StringBuffer("0").append(crc).toString();
            }
        }
        int length = str.length();
        String format = String.format("%04d", length);
        String s = new StringBuffer().append("##").append(format).append(str).append(crc).append("\r\n").toString();
        return s;
    }


    /**
     * @param temp 加密参数
     *             ##0101QN=20160801085857223;ST=32;CN=1062;PW=100000;MN=010000A8900016F000169DC0;Flag=5;CP=&&RtdInterval=30&&1C80
     *             QN=20160801085857223;ST=32;CN=1062;PW=100000;MN=010000A8900016F000169DC0;Flag=5;CP=&&RtdInterval=30&&
     *             1C80
     * @return
     * @Description: HJ212污染监测 CRC16校验算法
     * @author *****
     */
    public static String getCRC16(String temp) {
        Integer[] regs = new Integer[temp.length()];
        for (int i = 0; i < temp.length(); i++) {
            regs[i] = (int) temp.charAt(i);
        }
        int por = 0xFFFF;
        for (int j = 0; j < regs.length; j++) {
            por = por >> 8;
            por ^= regs[j];
            for (int i = 0; i < 8; i++) {
                if ((por & 0x01) == 1) {
                    por = por >> 1;
                    por = por ^ 0xa001;
                } else {
                    por = por >> 1;
                }
            }
        }
        return Integer.toHexString(por);
    }

}